package com.ruoyi.swla2.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.config.MinioConfigCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.swla2.domain.VideoSpectrum;
import com.ruoyi.swla2.service.IDeviceLedgerService;
import com.ruoyi.swla2.service.IVideoSpectrumService;
import liquibase.pro.packaged.S;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.swla2.mapper.FileVideoMapper;
import com.ruoyi.swla2.domain.FileVideo;
import com.ruoyi.swla2.service.IFileVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 音频文件Service业务层处理
 *
 * @author gdqt
 * @date 2025-06-26
 */
@Service
public class FileVideoServiceImpl extends ServiceImpl<FileVideoMapper, FileVideo> implements IFileVideoService
{
    @Autowired
    private FileVideoMapper fileVideoMapper;

    @Autowired
    private IVideoSpectrumService videoSpectrumService;

    @Autowired
    private IDeviceLedgerService deviceLedgerService;

    /**
     * 查询音频文件
     *
     * @param id 音频文件主键
     * @return 音频文件
     */
    @Override
    public FileVideo selectFileVideoById(Long id)
    {

        return fileVideoMapper.selectById(id);
    }

    @Override
    public Map<String, Object> selectFileVideoByParams(FileVideo fileVideo) {
        //url
        // json
        if(ObjectUtils.isEmpty(fileVideo.getDeviceNum()) && ObjectUtils.isEmpty(fileVideo.getReportDay())){
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<FileVideo> queryWrapperFileVideo = new LambdaQueryWrapper<>();
        queryWrapperFileVideo.eq(FileVideo::getDeviceNum, fileVideo.getDeviceNum())
                .eq(FileVideo::getReportDay, fileVideo.getReportDay());
        FileVideo fileVideoExist = fileVideoMapper.selectOne(queryWrapperFileVideo);
        if(ObjectUtils.isNotEmpty(fileVideoExist)){
            Map<String, Object>  result = new HashMap<>();

            Map<String, Object> alarmData = new HashMap<>();
            alarmData.put("reportDay", DateUtils.parseDateToStr("yyyy-MM-dd",fileVideoExist.getReportDay()));
            alarmData.put("status", fileVideoExist.getStatus());
            alarmData.put("dbValue", fileVideoExist.getDbValue());
            alarmData.put("noiseReference", fileVideoExist.getNoiseReference());
            result.put("alarmData", alarmData);


            result.put("wavePreUrl", FileUploadUtils.getDownLoadFileUrl(fileVideoExist.getFileUrl().substring(
                     MinioConfigCommon.getUrl().length() +MinioConfigCommon.getBucketName().length()  + 2
            )));

            result.put("fileName", fileVideoExist.getFileUrl().substring(
                    MinioConfigCommon.getUrl().length() +MinioConfigCommon.getBucketName().length()  + 2
            ));




            LambdaQueryWrapper<VideoSpectrum> queryWrapperVideoSpectrum = new LambdaQueryWrapper<>();
            queryWrapperVideoSpectrum.eq(VideoSpectrum::getFileVideoId, fileVideoExist.getId());
            VideoSpectrum videoSpectrum = videoSpectrumService.getOne(queryWrapperVideoSpectrum);
            if(ObjectUtils.isNotEmpty(videoSpectrum)){
                result.put("videoSpectrum", JSONArray.parseArray(videoSpectrum.getDetailData()));
            }




            return result;
        }
        return Collections.emptyMap();
    }

    /**
     * 查询音频文件列表
     *
     * @param fileVideo 音频文件
     * @return 音频文件
     */
    @Override
    public List<FileVideo> selectFileVideoList(FileVideo fileVideo)
    {
        LambdaQueryWrapper<FileVideo> queryWrapper = new LambdaQueryWrapper<>();
        if(fileVideo.getReportDay() != null){
            queryWrapper.eq(FileVideo::getReportDay,fileVideo.getReportDay());
        }
        if(!StringUtils.isEmpty(fileVideo.getDeviceNum())){
            queryWrapper.eq(FileVideo::getDeviceNum,fileVideo.getDeviceNum());
        }
        if(fileVideo.getLedgerId() != null){
            queryWrapper.eq(FileVideo::getLedgerId,fileVideo.getLedgerId());
        }
        if(!StringUtils.isEmpty(fileVideo.getStatus())){
            queryWrapper.eq(FileVideo::getStatus,fileVideo.getStatus());
        }
        if(!StringUtils.isEmpty(fileVideo.getDbValue())){
            queryWrapper.eq(FileVideo::getDbValue,fileVideo.getDbValue());
        }
        if(!StringUtils.isEmpty(fileVideo.getNoiseReference())){
            queryWrapper.eq(FileVideo::getNoiseReference,fileVideo.getNoiseReference());
        }
        if(!StringUtils.isEmpty(fileVideo.getFileUrl())){
            queryWrapper.eq(FileVideo::getFileUrl,fileVideo.getFileUrl());
        }
        if(!StringUtils.isEmpty(fileVideo.getFileName())){
            queryWrapper.eq(FileVideo::getFileName,fileVideo.getFileName());
        }
        if(!StringUtils.isEmpty(fileVideo.getNewFileName())){
            queryWrapper.eq(FileVideo::getNewFileName,fileVideo.getNewFileName());
        }
        if(!StringUtils.isEmpty(fileVideo.getOriginalFileName())){
            queryWrapper.eq(FileVideo::getOriginalFileName,fileVideo.getOriginalFileName());
        }
        return fileVideoMapper.selectList(queryWrapper);
    }

    /**
     * 新增音频文件
     *
     * @param fileVideo 音频文件
     * @return 结果
     */
    @Override
    public int insertFileVideo(FileVideo fileVideo)
    {
        return fileVideoMapper.insert(fileVideo);
    }

    /**
     * 修改音频文件
     *
     * @param fileVideo 音频文件
     * @return 结果
     */
    @Override
    public int updateFileVideo(FileVideo fileVideo)
    {
        fileVideo.setUpdateTime(DateUtils.getNowDate());
        return fileVideoMapper.updateById(fileVideo);
    }

    /**
     * 批量删除音频文件
     *
     * @param ids 需要删除的音频文件主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFileVideoByIds(Long[] ids)
    {
        return fileVideoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除音频文件信息
     *
     * @param id 音频文件主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFileVideoById(Long id)
    {
        return fileVideoMapper.deleteById(id);
    }

    public static void main(String[] args) {
        String fileU = "http://218.92.234.138:9000/dg-dma/2025/07/04/25011041-250701130046_20250704070008A002.wav";
        System.out.println(fileU.substring(
                "http://218.92.234.138:9000".length() + 1
        ));
    }
}
